﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using FeedAntGame.Control;

namespace FeedAntGame.Sprite.Event
{
    sealed class CandySprite : EventSprite
    {        
        private int power = 0;

        private int range = 0;

        private Candy.TYPE _type;
        /// <summary>
        /// default true
        /// </summary>
        private bool trackPointer = true;
        private bool startCheck = false;
        Texture2D rangeCircle;
        /************************************************************************/
        /*                      Get/Set                                                                     */
        /************************************************************************/

        /// <summary>
        /// Should Set when initialize candyFeed
        /// </summary>
        public bool TrackPointer
        {
            get { return trackPointer; }            
            set { trackPointer = value; }
        }        

        public int Range
        {
            get { return range; }
            set { range = value; }
        }

        public int Power
        {
            get { return power; }
            set 
            { 
                power = value;
                if (power <= 0)
                {
                    IsLive = false;
                }
            }
        }

        public Candy.TYPE Type
        {
            get { return _type; }
            set 
            { 
                _type = value;
                switch (_type)
                {
                    case Candy.TYPE.ORANGE:
                        Power = 20;
                        range = 50;
                        break;

                    // TODO: add more case here

                }
            }
        }
        /************************************************************************/
        /*                      Constructor                                                                     */
        /************************************************************************/
        public CandySprite(Texture2D rangeCircle, Texture2D clickBackground, Texture2D normalBackground, Texture2D hoverBackground,
            Texture2D disableBackground, Vector2 position, Point frameSize,
            int collisionOffset, Point sheetSize, Vector2 speed, string collisionCueName, float scale,
            Candy.TYPE type)
            : base(clickBackground,normalBackground,hoverBackground,disableBackground,position,frameSize,
            collisionOffset,sheetSize,speed,collisionCueName,scale)        
        {
            this._type = type;
            this.rangeCircle = rangeCircle;
            this.Origin = new Vector2(this.textureImage.Width / 2, this.textureImage.Height / 2);
        }

        /************************************************************************/
        /*                      Main functions                                                                     */
        /************************************************************************/

        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            if (State == STATE_TYPE.HOVER || State == STATE_TYPE.NORMAL)
            {
                startCheck = true;
            }
            
            if (trackPointer && startCheck)
            {
                this.Position = new Vector2(Mouse.GetState().X ,Mouse.GetState().Y);
                this.rotation += 0.1f;                 
                if (State == STATE_TYPE.CLICK)
                {
                    trackPointer = false;
                }
            }
            base.Update(gameTime, clientBounds);
            
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (State == STATE_TYPE.HOVER || trackPointer)
            {
                // draw range and power of candyFeed
                if(Visible)
                    spriteBatch.Draw(rangeCircle, new Rectangle(
                        (int)(this.position.X - range), 
                        (int)(this.position.Y  - range), range*2,range*2), Color.White);
                
                // draw string of power and range

            }
            base.Draw(gameTime, spriteBatch);
        }

        /// <summary>
        /// candy is flying widthScale pointer
        /// </summary>
        /// <returns></returns>
        public bool IsReadyToPutOnMap()
        {
            return !trackPointer;
        }

        public bool IsTracking()
        {
            return trackPointer && startCheck;   
        }
    }
}
